package org.kfive.nova.thread;

import java.util.concurrent.Exchanger;

/*
* ExchangeMessageExample.java
* Created  by  lizhanwu
* Created on 2016-5-31 19:11
*/
public class ExchangeMessageExample {

    private Exchanger<String> exchanger = new Exchanger<String>();

    private class ReadAThread implements Runnable{

        private Exchanger<String> exchanger;

        public ReadAThread(Exchanger<String> exchanger){
            this.exchanger = exchanger;
        }
        /**
         * When an object implementing interface <code>Runnable</code> is used
         * to create a thread, starting the thread causes the object's
         * <code>run</code> method to be called in that separately executing
         * thread.
         * <p/>
         * The general contract of the method <code>run</code> is that it may
         * take any action whatsoever.
         *
         * @see Thread#run()
         */
        public void run() {
            String message = "hello";
            System.out.println(Thread.currentThread().getName()+"before:"+message);
            try {
//                Thread.sleep(10000);
                message = exchanger.exchange(message);
                System.out.println(Thread.currentThread().getName()+"after:"+message);
                message = exchanger.exchange("111");
                System.out.println(Thread.currentThread().getName()+"after:"+message);
            } catch (InterruptedException e) {
                e.printStackTrace();
            } finally {
            }
        }
    }

    private class ReadBThread implements Runnable{

        private Exchanger<String> exchanger;

        public ReadBThread(Exchanger<String> exchanger){
            this.exchanger = exchanger;
        }
        /**
         * When an object implementing interface <code>Runnable</code> is used
         * to create a thread, starting the thread causes the object's
         * <code>run</code> method to be called in that separately executing
         * thread.
         * <p/>
         * The general contract of the method <code>run</code> is that it may
         * take any action whatsoever.
         *
         * @see Thread#run()
         */
        public void run() {
            String message = "world";
            System.out.println(Thread.currentThread().getName()+"before:"+message);
            try {
                message = exchanger.exchange(message);
                System.out.println(Thread.currentThread().getName()+"after:"+message);
                message = exchanger.exchange("222");
                System.out.println(Thread.currentThread().getName()+"after:"+message);
            } catch (InterruptedException e) {
                e.printStackTrace();
            } finally {
            }
        }
    }

    public void test(){
        new Thread(new ReadAThread(this.exchanger)).start();
        new Thread(new ReadBThread(this.exchanger)).start();
    }
    public static void main(String[] args) {
        ExchangeMessageExample exchangeMessageExample = new ExchangeMessageExample();
        exchangeMessageExample.test();
    }
}
